/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hydms.registration.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.hydms.illegalregistration.entity.DmsIllegalRegistration;
import com.thinkgem.jeesite.modules.hydms.registration.entity.DmsRegistration;
import com.thinkgem.jeesite.modules.hydms.registration.service.DmsRegistrationService;

/**
 * 报修登记Controller
 * @author 刘健
 * @version 2017-04-05
 */
@Controller
@RequestMapping(value = "${adminPath}/registration/dmsRegistration")
public class DmsRegistrationController extends BaseController {

	@Autowired
	private DmsRegistrationService dmsRegistrationService;
	
	@ModelAttribute
	public DmsRegistration get(@RequestParam(required=false) String id) {
		DmsRegistration entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dmsRegistrationService.get(id);
		}
		if (entity == null){
			entity = new DmsRegistration();
		}
		return entity;
	}
	
	
	/**
	 * 导出用户数据
	 * @param DmsIllegalRegistration
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("registration:dmsRegistration:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(DmsRegistration dmsRegistration, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "报修信息"+DateUtils.getDate("yyyyMMdd")+".xlsx";
            Page<DmsRegistration> page = dmsRegistrationService.findPage(new Page<DmsRegistration>(request, response, -1), dmsRegistration);
    		new ExportExcel("报修信息", DmsRegistration.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "报修信息导出失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/registration/dmsRegistration/list?repage";
    }
	
	/**
	 * 宿舍报修登记列表
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("registration:dmsRegistration:view")
	@RequestMapping(value = {"list", ""})
	public String list(DmsRegistration dmsRegistration, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DmsRegistration> page = dmsRegistrationService.findPage(new Page<DmsRegistration>(request, response), dmsRegistration); 
		model.addAttribute("page", page);
		return "hydms/registration/dmsRegistrationList";
	}

	/**
	 * 宿舍报修登记添加
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("registration:dmsRegistration:view")
	@RequestMapping(value = "form")
	public String form(DmsRegistration dmsRegistration, Model model) {
		model.addAttribute("dmsRegistration", dmsRegistration);
		return "hydms/registration/dmsRegistrationForm";
	}

	/**
	 * save
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("registration:dmsRegistration:edit")
	@RequestMapping(value = "save")
	public String save(DmsRegistration dmsRegistration, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dmsRegistration)){
			return form(dmsRegistration, model);
		}
		dmsRegistrationService.save(dmsRegistration);
		addMessage(redirectAttributes, "保存报修登记成功");
		return "redirect:"+Global.getAdminPath()+"/registration/dmsRegistration/?repage";
	}
	
	/**
	 * delete
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("registration:dmsRegistration:edit")
	@RequestMapping(value = "delete")
	public String delete(DmsRegistration dmsRegistration, RedirectAttributes redirectAttributes) {
		dmsRegistrationService.delete(dmsRegistration);
		addMessage(redirectAttributes, "删除报修登记成功");
		return "redirect:"+Global.getAdminPath()+"/registration/dmsRegistration/?repage";
	}

}